import 'dart:convert';

import 'package:dio/dio.dart';
/**
 * 添加银行卡
 * author wim
 */
import 'package:flutter/material.dart';
import 'package:flutter_picker/Picker.dart';
import '../../util/Toast.dart';
import '../../entity/user_bank_entity.dart';
import '../../http/Api.dart';
import '../../http/HttpUtil.dart';
import '../../util/AppSaveInfoUtil.dart';
import '../../util/LoadingUtil.dart';
import '../../util/SignUtil.dart';
// import 'package:xlive_switch/xlive_switch.dart';
import '../../base/Constant.dart';
import 'dart:ui';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AddBankPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return AddBankView();
  }
}

class AddBankView extends State<AddBankPage> {
  List<UserBankListDataData> dataList = new List();
  TextEditingController controller = new TextEditingController();
  String token = "";
  var PickerData = [];
  List<String> PickerData2 = new List();
  String bank_id = "", bank_name = "", account = "", name = "";
  bool isCheck = false;
  @override
  void initState() {
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
    });
    loadList();
  }

  loadList() async {
    Map map = new Map();
    map['sign_type'] = createSignType(Api.URL_BANK_LIST);
    Response<String> response =
        await HttpUtil().post(Api.URL_BANK_LIST, data: getSign(map));
    Map userMap = json.decode(response.data);
    if (userMap['code'] != 1) {
      // Fluttertoast.showToast(msg: userMap['msg']);
      Toast.toast(context,msg: userMap['msg']);
      return;
    }
    UserBankListEntity homeListEntity =
        new UserBankListEntity.fromJson(userMap);
    if (mounted)
      setState(() {
        dataList.addAll(homeListEntity.data.datas);
        for (int i = 0; i < dataList.length; i++) {
          PickerData.add("\"" + dataList[i].bank_name + "\"");
        }
      });
  }

  showCate() {
    new Picker(
        adapter: PickerDataAdapter<String>(
            pickerdata: new JsonDecoder().convert(PickerData.toString())),
        hideHeader: true,
        title: new Text("选择银行"),
        cancelText: "取消",
        cancelTextStyle: TextStyle(color: Colors.grey),
        confirmText: "确定",
        confirmTextStyle: TextStyle(color: mainColor),
        onConfirm: (Picker picker, List value) {
          bank_id = dataList[value[0]].bank_id;
          bank_name = picker.getSelectedValues()[0];
          controller.text = bank_name;
          setState(() {});
        }).showDialog(context);
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "添加银行卡",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0.2,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
      ),
      resizeToAvoidBottomPadding: false, //输入框抵住键盘
      body: new Container(
        color: divideColor,
        child: new Column(
          children: <Widget>[
            Container(
              height: ScreenUtil.getInstance().setHeight(DP_10),
            ),
            new Container(
              padding: EdgeInsets.all(
                ScreenUtil.getInstance().setHeight(DP_15),
              ),
              color: white,
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('银行',
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  ),
                  Expanded(
                    flex: 3,
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        showCate();
                      },
                      child: new TextField(
                        maxLines: 1,
                        enabled: false,
                        cursorColor: mainColor,
                        controller: controller,
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE)),
                        keyboardAppearance: Brightness.light,
                        decoration: new InputDecoration(
                            hintText: "选择银行",
                            border: InputBorder.none,
                            hintStyle: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(NORMAL_FONT_SIZE))),
                      ),
                    ),
                  )
                ],
              ),
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(0, 1, 0, 0),
              padding: EdgeInsets.all(
                ScreenUtil.getInstance().setHeight(DP_15),
              ),
              color: Colors.white,
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text(
                      '持卡人',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ),
                  Expanded(
                    flex: 3,
                    child: new TextField(
                      maxLines: 1,
                      cursorColor: mainColor,
                      onChanged: (value) {
                        name = value;
                      },
                      keyboardAppearance: Brightness.light,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                      decoration: new InputDecoration(
                          hintText: "持卡人姓名",
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                  )
                ],
              ),
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(0, 1, 0, 0),
              padding: EdgeInsets.all(
                ScreenUtil.getInstance().setHeight(DP_15),
              ),
              color: Colors.white,
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('卡号',
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  ),
                  Expanded(
                    flex: 3,
                    child: new TextField(
                      maxLines: 1,
                      onChanged: (value) {
                        account = value;
                      },
                      keyboardAppearance: Brightness.light,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                      cursorColor: mainColor,
                      decoration: new InputDecoration(
                          hintText: "银行卡号",
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                  )
                ],
              ),
            ),
            // new Container(
            //   margin: EdgeInsets.fromLTRB(0, 1, 0, 0),
            //   padding: EdgeInsets.all(
            //     ScreenUtil.getInstance().setHeight(DP_15),
            //   ),
            //   color: Colors.white,
            //   child: Row(
            //     children: <Widget>[
            //       Expanded(
            //         child: Text(
            //           '默认卡号',
            //           style: TextStyle(
            //               fontSize:
            //                   ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
            //               color: Colors.black87),
            //         ),
            //       ),
            //       XlivSwitch(
            //         value: isCheck,
            //         activeColor: mainColor,
            //         unActiveColor: Colors.grey, // 激活时原点颜色
            //         onChanged: (bool val) {
            //           // checkInform();
            //           isCheck = !isCheck;
            //           setState(() {});
            //         },
            //       )
            //     ],
            //   ),
            // ),
            new Container(
              width: double.infinity,
              height: ScreenUtil.getInstance().setHeight(DP_80),
              margin: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_80)),
              decoration: new BoxDecoration(
                color: Color.fromARGB(255, 255, 140, 16),
                  borderRadius: BorderRadius.circular(
                      ScreenUtil.getInstance().setHeight(DP_80))),
              padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
              child: new FlatButton(
                child: new Padding(
                  padding: EdgeInsets.all(0.0),
                  child: new Text(
                    "确认添加",
                    style: new TextStyle(
                        color: Colors.white,
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                  ),
                ),
                onPressed: () {
                  if (bank_id == "") {
                    // Fluttertoast.showToast(msg: '请选择银行');
                    Toast.toast(context,msg: '请选择银行');
                    return;
                  }
                  if (name == "") {
                    // Fluttertoast.showToast(msg: '请输入真实姓名');
                    Toast.toast(context,msg: '请输入真实姓名');
                    return;
                  }
                  if (account == "") {
                    // Fluttertoast.showToast(msg: '请输入卡号或账号');
                    Toast.toast(context,msg:'请输入卡号或账号');
                    return;
                  }
                  showDialog(
                      context: context,
                      barrierDismissible: false,
                      builder: (_) {
                        return new NetLoadingDialog(
                          loadingText: "添加中...",
                          bgColor: Color.fromARGB(255, 0, 0, 0),
                          requestCallBack: null,
                          outsideDismiss: false,
                        );
                      });
                  register();
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  register() async {
    Map map = new Map();
    map['token'] = token;
    map['bank_id'] = bank_id;
    map['account'] = account;
    map['truename'] =name;
    map['sign_type'] = createSignType(Api.URL_BANKCARD_ADD);
    Response<String> response =
        await HttpUtil().post(Api.URL_BANKCARD_ADD, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Navigator.of(context).pop(1);
    // Fluttertoast.showToast(msg: msg);
     Toast.toast(context,msg:msg);
    if (code == 1 && mounted) {
      Navigator.of(context).pop("1");
    }
  }
}
